package com.dzu.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dzu.common.utils.PageUtils;
import com.dzu.common.utils.Query;
import com.dzu.gulimall.product.dao.SkuInfoDao;
import com.dzu.gulimall.product.entity.SkuImagesEntity;
import com.dzu.gulimall.product.entity.SkuInfoEntity;
import com.dzu.gulimall.product.entity.SpuInfoDescEntity;
import com.dzu.gulimall.product.service.*;
import com.dzu.gulimall.product.vo.SkuItemSaleAttrVo;
import com.dzu.gulimall.product.vo.SkuItemVo;
import com.dzu.gulimall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private SkuImagesService skuImagesService;

    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();

        /**
         * key: '华为',//检索关键字
         * catalogId: 0,
         * brandId: 0,
         * min: 0,
         * max: 0
         */
        String key = (String) params.get("key");
        if (StringUtils.isNotEmpty(key)) {
            queryWrapper.and(wq -> wq.eq("sku_id", key).or().like("sku_name", key));
        }

        String catalogId = (String) params.get("catalogId");
        if (StringUtils.isNotEmpty(catalogId)) {
            queryWrapper.eq("catalog_id",catalogId);
        }

        String brandId = (String) params.get("brandId");
        if (StringUtils.isNotEmpty(brandId)) {
            queryWrapper.eq("brand_id",brandId);

        }

        String min = (String) params.get("min");
        if (StringUtils.isNotEmpty(min)) {
            queryWrapper.ge("price", min);

        }
        String max = (String) params.get("max");
        if (StringUtils.isNotEmpty(max)) {
            try {
                BigDecimal bigDecimal = new BigDecimal(max);
                if (bigDecimal.compareTo(new BigDecimal(0)) == 1){
                    queryWrapper.le("price",max);
                }
            }catch (Exception e){

            }

        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);

    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {

        List<SkuInfoEntity> list = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id",spuId));
        return list;
    }

    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        // 异步编排
        SkuItemVo vo = new SkuItemVo();

        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            //    1、sku基本信息获取、pms_sku_inf o
            SkuInfoEntity skuInfo = getById(skuId);
            vo.setInfo(skuInfo);
            return skuInfo;
        }, threadPoolExecutor);


        CompletableFuture<Void> saleFuture = infoFuture.thenAcceptAsync((res) -> {
            //    3、获取spu的销售属性组合。
            List<SkuItemSaleAttrVo> skuSaleAttr = skuSaleAttrValueService.getSaleAttrsBySpuId(res.getSpuId());
            vo.setSaleAttr(skuSaleAttr);
        }, threadPoolExecutor);

        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((res) -> {
            //    4、获取spu的介绍\pms_spu_info_desc
            SpuInfoDescEntity spuInfoDesc = spuInfoDescService.getById(res.getSpuId());
            vo.setDesc(spuInfoDesc);
        }, threadPoolExecutor);

        CompletableFuture<Void> groupFuture = infoFuture.thenAcceptAsync((res) -> {
            //    5、spu的规格参数信息
            List<SpuItemAttrGroupVo> groupAttrs = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            vo.setGroupAttrs(groupAttrs);
        }, threadPoolExecutor);

        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            //    2、sku图片信息、pms_sku_images
            List<SkuImagesEntity> images = skuImagesService.getImagesBySkuId(skuId);
            vo.setImages(images);
        }, threadPoolExecutor);

        // 等到所有的任务都完成，才返回vo
        CompletableFuture.allOf(saleFuture, descFuture, groupFuture, imageFuture).get();


        return vo;

    }
}
